package com.zhuimeng.mysql_rocketmq.listener;

import com.zhuimeng.mysql_rocketmq.dao.SkillOrderRepository;
import com.zhuimeng.mysql_rocketmq.pojo.SkillOrder;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Component
@RocketMQMessageListener(topic = "KILLMESSAGE",consumerGroup = "mysqlRocketMq")
public class MysqlRocketListener implements RocketMQListener<String> {
    @Autowired
    private SkillOrderRepository skillOrderRepository;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Override
    @Transactional
    public void onMessage(String userId) {
        SkillOrder skillOrder = new SkillOrder();
        skillOrder.setOrderId(UUID.randomUUID().toString().replace("-",""));
        skillOrder.setUserId(userId);
        skillOrder.setContent("手机");
        skillOrder.setOrderStatus("未支付");
        skillOrderRepository.saveAndFlush(skillOrder);
        Message message = new Message();
        message.setBody(userId.getBytes());
        final org.springframework.messaging.Message springMessage = RocketMQUtil.convertToSpringMessage(message);
        rocketMQTemplate.asyncSend("DELETEORDER", springMessage, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {

            }

            @Override
            public void onException(Throwable throwable) {

            }
        },5000,5);
    }
}
